appCtrl.controller('FormCtrl', function($scope, $rootScope, $state, UtilsService) {
    if (!$rootScope.dist) {
      $rootScope.$broadcast('loading:showMessage', {
        message: '请选择所在小区'
      });
      $state.go('district.list');
      return;
    }
  })

  .controller('FormInfoCtrl', function($scope, $rootScope, $stateParams, UtilsService, DistService) {
    $scope.dist = $rootScope.dist;
    DistService.getInfoDetail({
      dist_id: $scope.dist.district_id,
      property_id: $stateParams.id,
    }).$promise.then(function(response) {
      if (UtilsService.isResponseOk(response)) {
        $scope.info = response.data.info;
      }
    });
  })
  .controller('FormInfoListCtrl', function($scope, $rootScope, UtilsService, DistService) {
    $scope.dist = $rootScope.dist;
    DistService.getAllInfos({
      dist_id: $scope.dist.district_id
    }).$promise.then(function(response) {
      if (UtilsService.isResponseOk(response)) {
        $scope.infos = response.data.infos;
      }
    });
  })
  .controller('FormSecondHandCtrl', function($scope, $rootScope, $ionicPopup, $stateParams, UtilsService, SecondhandService) {
    $scope.vm = this
    var _this = this
    this.showBigImg = false
    SecondhandService.get({
      id: $stateParams.id
    }).$promise.then(function(response) {
      if (UtilsService.isResponseOk(response)) {
        $scope.second = response.data.second;
        $scope.comments = response.data.comments;
      }
    })

    $scope.showBigImg = function (imgurl) {
      console.log(imgurl)
      _this.bigsrc = imgurl
      _this.showBigImg = true
    }

    $scope.goBack = function () {
      UtilsService.goBack();
    }

    $scope.addComment = function(toComment) {
      // 获取评论
      var contentPopup = $ionicPopup.show({
        template: '<textarea ng-model="con" id="con" rows="6"></textarea>',
        title: '请输入回复内容',
        subTitle: toComment ? toComment.from_user.name + ':' + toComment.content : '',
        scope: $scope,
        buttons: [{
          text: '取消'
        }, {
          text: '<b>回复</b>',
          type: 'button-assertive',
          onTap: function(e) {
            var con = window.document.getElementById('con').value;
            if (!con) {
              //don't allow the user to close unless he enters wifi password
              e.preventDefault();
            } else {
              return con;
            }
          }
        }]
      });

      contentPopup.then(function(content) {
        if (!content) {
          return;
        }
        var comment = {};
        comment.content = content;
        if (toComment) {
          // 评论给另一个人
          comment.to_user_id = toComment.from_user.user_id;
        }
        $scope.second.secondhand_product_id,
          console.log(comment);

        SecondhandService.addComment({
          id: $scope.second.secondhand_product_id,
          comment: comment
        }).$promise.then(function(response) {
          if (UtilsService.isResponseOk(response)) {
            // comment = response.data.comment;
            SecondhandService.getAllComments({
              id: $scope.second.secondhand_product_id
            }).$promise.then(function(response) {
              if (UtilsService.isResponseOk(response)) {
                $scope.comments = response.data.comments;
              }
            })
          }
        })
      })

    }
  })
  .controller('FormSecondHandListCtrl', function($scope, $rootScope, $state, UtilsService, ArepoService) {
    $scope.dist = $rootScope.dist;

    if (!$rootScope.dist) {
      $rootScope.$broadcast('loading:showMessage', {
        message: '请选择所在小区'
      });
      $state.go('district.list');

    }
    $scope.goBack = function() {
      UtilsService.goBack();
    }
    ArepoService.getSecondhand({
      id: $scope.dist.a_repository_id
    }).$promise.then(function(response) {
      $scope.seconds = response.data.seconds;
    })
  })
  .controller('FormAddSecondCtrl', function($scope, $rootScope, $state, UtilsService, ArepoService) {
    $scope.dist = $rootScope.dist;

    $scope.goBack = function () {
      UtilsService.goBack();
    }

    $scope.public = function() {
      var content = document.getElementById('secondcontent').value;

      //			if ($scope.srclist.length <= 0) {
      //				plus.nativeUI.alert("没有添加上传文件！");
      //				return;
      //			}
      console.log("开始上传：");
      var wt = plus.nativeUI.showWaiting();
      var task = plus.uploader.createUpload(baseUrl + '/arepo/' + $scope.dist.a_repository_id + '/secondhand', {
          method: "POST"
        },
        function(t, status) { //上传完成
          wt.close();
          if (status == 200) {
            console.log("上传成功：" + t.responseText);
            var response = angular.fromJson(t.responseText);
            if (UtilsService.isResponseOk(response)) {
              // $state.transitionTo('form.secondhand', {
              //   id: response.data.id
              // }, {
              //   reload: true
              // });
              UtilsService.goBack();
              $rootScope.$broadcast('loading:showMessage', {
                message: '发布成功，管理员通过后，才可显示'
              });
            } else {
              $rootScope.$broadcast('loading:showMessage', {
                message: response.state.message
              });
            }
          }
        }

      );
      console.log(content);
      task.addData("content", content);
      for (var i = 0; i < $scope.srclist.length; i++) {
        var f = $scope.srclist[i];
        task.addFile(f, {
          key: i
        });
      }

      task.start();
      console.log(angular.toJson(task));
    }

    $scope.srclist = [];
    $scope.showActionSheet = function() {
      if ($scope.srclist.length >= 3) {
        return;
      }
      //  	outSet( "弹出系统选择按钮框：" );
      var bts = [{
        title: "拍照上传"
      }, {
        title: "相册中选择"
      }];
      plus.nativeUI.actionSheet({
          title: "请选择",
          cancel: "取消",
          buttons: bts
        },

        function(e) {
          switch (e.index) {
            case 1:
              // 拍照上传
              console.log("开始拍照：");
              var cmr = plus.camera.getCamera();
              cmr.captureImage(function(p) {
                console.log("成功：" + p);
                plus.io.resolveLocalFileSystemURL(p, function(entry) {
                  $scope.srclist.push(entry.toLocalURL());
                }, function(e) {
                  console.log("读取拍照文件错误：" + e.message);
                });
              }, function(e) {
                console.log("失败：" + e.message);
              }, {
                filename: "_doc/camera/",
                index: 1
              });
              break;
            case 2:
              // 系统相册
              plus.gallery.pick(function(path) {
                plus.io.resolveLocalFileSystemURL(path, function(entry) {
                  $scope.srclist.push(entry.toLocalURL());
                }, function(e) {
                  console.log("读取拍照文件错误：" + e.message);
                });
              }, function(e) {
                console.log("取消选择图片");
              }, {
                filter: "image"
              });
              break;
          }
        }
      );
    }
  });
